﻿// <copyright file="Toolbox.xaml.cs" company="Marcin Wosik">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Marcin Wosik</author>
// <email>mawo11@gmail.com</email>
// <date>2010-02-25</date>
// <summary></summary>
namespace GameOfLife.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using GameOfLife.GameData;
    using GameOfLife.Interfaces;

    /// <summary>
    /// Control to change the game setting 
    /// </summary>
    public partial class Toolbox : UserControl, IToolbox
    {
        #region Constructors

        /// <summary>
        /// Default contructor of class
        /// </summary>
        public Toolbox()
        {
            InitializeComponent();
            
        }

        #endregion Constructors

        #region Events

        /// <summary>
        /// Event for publishing command of pressed button 
        /// </summary>
        public event EventHandler<CommandEventArgs<ToolboxCommand>> Command;

        #endregion Events

        #region Properties

        /// <summary>
        /// Copy of game settings for editing
        /// </summary>
        public GameSettings Data
        {
            get
            {
                GameSettings settings = LayoutRoot.DataContext as GameSettings;
                if (settings == null)
                {
                    settings = GameSettings.Default();
                }
                return settings;
            }
            set
            {
                this.LayoutRoot.DataContext = value;
            }
        }

        /// <summary>
        /// Enable/disable controls on the form to prevent change some setting in simulation
        /// </summary>
        public bool EditMode
        {
            get
            {
                return false;
            }
            set
            {
                this.Rows.IsEnabled = value;
                this.Columns.IsEnabled = value;
                this.Rule.IsEnabled = value;
                this.PrepareButton.IsEnabled = value;
                this.RandomButton.IsEnabled = value;
                this.ConnectionTypeList.IsEnabled = value;
                this.ColorList.IsEnabled = value;
            }
        }

        #endregion Properties

        #region Methods

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
        protected virtual void FireCommand(ToolboxCommand command)
        {
            if (this.Command != null)
            {
                this.Command(this, new CommandEventArgs<ToolboxCommand>(command));
            }
        }

        private void ColorListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.FireCommand(ToolboxCommand.UpdateColoringMode);
        }

        private void ConnectionTypeListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.FireCommand(ToolboxCommand.UpdateConnectionType);
        }

        private void CreateButtonClick(object sender, RoutedEventArgs e)
        {
            this.FireCommand(ToolboxCommand.Draw);
        }

        private void RandomButtonClick(object sender, RoutedEventArgs e)
        {
            this.FireCommand(ToolboxCommand.Random);
        }

        private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.FireCommand(ToolboxCommand.UpdateTimer);
        }

        #endregion Methods
    }
}